package com.demoo.zjsu.controller.smartwebsite;

import com.demoo.zjsu.dao.smartwebsite.SmartWebSelectDao;
import com.demoo.zjsu.model.smartwebsite.PageOne;
import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jshjsh on 2016/8/25.
 * 出了了教务处，首页，保卫处，招生办四个主页面
 */
@Controller
public class CommonController {


    @Autowired
    SmartWebSelectDao dao;



    @RequestMapping("/smartweb/pagetwo/{page}")
    public ModelAndView toPageTwo(@PathVariable String page,
                                  @RequestParam("category") String category,
                                  @RequestParam("sort") int sort) {
        List<PageTwo> pageTwos = dao.findPageTwoSort(category, sort, sort + 10);
        int size = pageTwos.size();
        return new ModelAndView("smartweb/" + page)
                .addObject("page", pageTwos)
                .addObject("category", category)
                .addObject("sort", sort)
                .addObject("size",size);
    }


    /**
     * 首页的处理
     *
     * @return
     */
    @RequestMapping("/smartweb/main")
    public ModelAndView getMainPage() {
        List<PageTwo> pageTwos6 = dao.selectLimitPageTwoCategory("首页轮播图", 3);
        List<PageTwo> pageTwos = dao.selectLimitPageTwoCategory("商大新闻", 5);
        List<PageTwo> pageTwos1 = dao.selectLimitPageTwoCategory("公告", 8);
        List<PageTwo> pageTwos2 = dao.selectLimitPageTwoCategory("学术", 8);
        List<PageTwo> pageTwos3 = dao.selectLimitPageTwoCategory("交流", 8);
        List<PageTwo> pageTwos4 = dao.selectLimitPageTwoCategory("文体", 8);
        List<PageTwo> pageTwos5 = dao.findByCategoryAndTop("商大新闻");
        getAbber(pageTwos5,20);
        ModelAndView modelAndView = new ModelAndView("smartweb/main").addObject("news", pageTwos)
                .addObject("gonggao", pageTwos1).addObject("xueshu", pageTwos2)
                .addObject("jiaoliu", pageTwos3).addObject("wenti", pageTwos4)
                .addObject("photo", pageTwos5).addObject("lunbo", pageTwos6);
        return modelAndView;
    }

    public void getAbber(List<PageTwo> pageTwos,int size) {
        for (PageTwo pageTwo : pageTwos) {
            PageTwoContent content = pageTwo.getContent();
            String abbr = content.getContent().replaceAll("<[^>]*>", "").trim();
            if (abbr.length() > size) {
                content.setContent(abbr.substring(0, size));
            } else {
                content.setContent(abbr);
            }
        }
    }


    /**
     * 教务处的处理
     *
     * @return
     */
    @RequestMapping("/smartweb/jiaowuchu")
    public ModelAndView getJiaowu() {
        List<PageTwo> pageTwos6 = dao.selectLimitPageTwoCategory("首页轮播图", 3);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("category","最新动态");
        map.put("sort", 3);
        map.put("limit", 5);
        List<PageTwo> pageTwos = dao.findPageTwoLimitSort(map);
        List<PageTwo> pageTwos1 = dao.selectLimitPageTwoCategory("学生专区", 8);
        List<PageTwo> pageTwos2 = dao.selectLimitPageTwoCategory("教师专区", 8);
        List<PageTwo> pageTwos3 = dao.findByCategoryWithOutTop("最新动态");
        getAbber(pageTwos3,40);
        ModelAndView modelAndView = new ModelAndView("smartweb/jiaowuchu").addObject("recent", pageTwos3)
                .addObject("student", pageTwos1).addObject("teacher", pageTwos2)
                .addObject("lunbo", pageTwos6).addObject("news", pageTwos);
        return modelAndView;
    }


    /**
     * 招办的处理
     *
     * @return
     */
    @RequestMapping("/smartweb/zhaosheng")
    public ModelAndView getZhaoSheng() {
        List<PageTwo> pageTwos6 = dao.selectLimitPageTwoCategory("首页轮播图", 3);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("category","报考指南");
        map.put("sort", 3);
        map.put("limit", 5);
        List<PageTwo> pageTwos = dao.findPageTwoLimitSort(map);
        List<PageTwo> pageTwos1 = dao.selectLimitPageTwoCategory("最新消息", 8);
        List<PageTwo> pageTwos2 = dao.selectLimitPageTwoCategory("走进商大", 8);
        List<PageTwo> pageTwos3 = dao.findByCategoryWithOutTop("报考指南");
        getAbber(pageTwos3,40);
        ModelAndView modelAndView = new ModelAndView("smartweb/zhaosheng").addObject("recent", pageTwos3)
                .addObject("student", pageTwos1).addObject("teacher", pageTwos2)
                .addObject("lunbo", pageTwos6).addObject("news", pageTwos);
        return modelAndView;
    }


}